package cn.itsource.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.GetRedisLoginVoUtil;
import cn.itsource.config.alipay.AlipayService;
import cn.itsource.order.domain.OrderAddress;
import cn.itsource.order.domain.OrderDTO;
import cn.itsource.order.domain.TOrderAdopt;
import cn.itsource.order.mapper.OrderAdapterMapper;
import cn.itsource.order.mapper.OrderAddressMapper;
import cn.itsource.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.jaxb.OrderAdapter;
import org.springframework.stereotype.Service;
//交给spring管理
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderAddressMapper orderAddressMapper;
    @Autowired
    OrderAdapterMapper orderAdapterMapper;
    @Autowired
    AlipayService alipayService;
    /**
     * 订单下单的业务逻辑
     * @param orderDTO
     * @return
     */
    @Override
    public String createorder(OrderDTO orderDTO) {
        //1.先新增订单地址
        OrderAddress orderAddress = BeanUtil.copyProperties(orderDTO.getUserAddress(), OrderAddress.class);
        orderAddress.setId(null);
        orderAddressMapper.insert( orderAddress );
        //2.新增订单数据
        TOrderAdopt tOrderAdopt = new TOrderAdopt(orderDTO);
        tOrderAdopt.setUserId(GetRedisLoginVoUtil.getUserId());
        tOrderAdopt.setAddressId(orderAddress.getId());
        orderAdapterMapper.insert(tOrderAdopt);
        //去发起支付宝的调用
        return alipayService.createPayUrl(tOrderAdopt.getOrderSn(),orderDTO.getPetName(),tOrderAdopt.getPrice().doubleValue(), tOrderAdopt.getDigest());
    }
}
